
import os
import logging
import psycopg2
import psycopg2.extras
from flask import Flask
from flask import g
from flask import request
from flask import jsonify
from flask_cors import CORS
from database import SqliteSingleton
from flask_blueprint.wx_blueprint import wx_blueprint
from flask_blueprint.web_blueprint import web_blueprint
app = Flask(__name__)
CORS(app)
logger = logging.getLogger(__name__)


# init database
with SqliteSingleton() as conn:
    script_path = "database/init_db.sql"  
    logger.info(os.path.exists(script_path))
    if os.path.exists(script_path):
        logger.info("init database from script")
        with open(script_path, 'r', encoding='utf-8') as file:
            cur = conn.cursor()
            sql_script = file.read()
            cur.executescript(sql_script)
            conn.commit()  # 提交事务
    

app.logger.setLevel(logging.DEBUG)
app.register_blueprint(wx_blueprint)
app.register_blueprint(web_blueprint)




@app.route('/')
def index():
    return jsonify({'message': 'Welcome to Elite Notice'})




if __name__ == '__main__':
    
    app.run(debug=True, port=5001)
    




